#include "driverh.h"
#include "dataformat.h"

#ifndef __COMMON_H
#define __COMMON_H


#define CMN_BCD_MAX_NUM     0x99

extern const INT8U BitsPattern[8];
extern const INT8U BitsMask[8];
extern const INT16U WordBitsPattern[16];
extern const INT16U WordBitsMask[16];

INT16U GetCRCBj(INT8U *message, INT16U length);
INT16U CmnGetCRCTT(INT8U *pBuf, INT32U size, INT16U crc);
INT16U CmnGetCRC16(INT8U *p, INT32U len, INT16U start);
INT8U CmnGetCsByte(INT8U *pBuf, INT16U size);

#define IsBuffZero(x, y) CmnIsBuffFillxx(x, y, 0x00)
#define IsBuffNull(x, y) CmnIsBuffFillxx(x, y, 0xFF)
#define IsBuffFill(x, y) CmnIsBuffFillxx(x, y, METER_DATA_FILL_CH)
BOOL CmnIsBuffFillxx(INT8U *ptr, INT16U size, INT8U data);
BOOL IsBuffExistxx(INT8U *ptr, INT16U size, INT8U data);

void CmnAscs2Hexs(INT8U * pAsciiData, INT16U len, INT8U * pHexBuff);
void CmnHex2Asc(INT8U * pHexBuff, INT16U len, INT8U * pAsciiData);
INT8U CmnBcd2Byte(INT8U bcd);
INT8U CmnByte2Bcd(INT8U ch);
INT16U CmnBcd2Word(INT16U bcd);
INT16U CmnWord2Bcd(INT16U num);
void CmnWord2BcdL(INT16U num, INT8U *bcd);
INT32U CmnBcd2Long(INT8U *bcd, INT8U size);
void CmnLongToBcd(INT64U num, INT8U *bcd, INT8U size);
void CmnLong2Bcd(INT64U num, INT8U *bcd, INT8U size);
void CmnBcdAdd(INT8U *pDes, INT8U *pSrc, INT32 size);
void CmnBcdSub(INT8U *pDes, INT8U *pSrc, INT32 size);
INT32 CmnBcdCmp(INT8U *pBuff1, INT8U *pBuff2, INT32 size);
BOOL CmnIsBcd(INT8U *ptr, INT16U size);
INT32U CmnGetMaxOfABC(INT32U a, INT32U b, INT32U c);
INT32U CmnGetMinOfABC(INT32U a, INT32U b, INT32U c);
BOOL CmnHex2CompressBcd(INT8U *SrcBuff, INT8U SrcDigits, INT8U SrcDecimalDigits,
                        INT8U *DestBuff, INT8U DestDigits, INT8U DestDecimalDigits);
BOOL CmnCompressBcd2Hex(INT8U * SrcBuff, INT8U SrcDigits, INT8U SrcDecimalDigits,
                        INT8U DestDigits, INT8U DestDecimalDigits, INT8U *DestBuff);
BOOL CmnAdjustDigits(INT8U * SrcBuff, INT8U SrcDigits, INT8U SrcDecimalDigits,
                     INT8U *DestBuff,        INT8U DestDigits, INT8U DestDecimalDigits);
void CmnMaskByteBit7(INT8U *pByte);
void CmnMaskBit7(INT8U *pBuff, INT32 length);
BOOL CmnIsDigitalAsc(INT8U *pAsciiData, INT32 len);
void CmnAscs2Bcds(INT8U *pAsciiData, INT32 len, INT8U *pBCDBuff);
void CmnBcds2Ascs(INT8U *pBCDBuff, INT32 bufsize, INT8U *pAsciiData, INT32 decimal);
INT32 CmnGetFormat15Minutes(LCDataFormat15Type *p);
INT32 CmnBinarySearch(INT16U key, INT8U *pBuff, INT32 UnitSize, INT32 size);
void Anticpy(INT8U *bDstAddr, INT8U *bSrcAddr, INT16U wLen);
int DateTimeToFmt1(DateTimeType dt, LCDataFormat01Type* pFormat);

#endif

